package com.ejuornal.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.EditorCommit;
import com.ejuornal.bean.Page;
import com.ejuornal.dao.EditorCommitDao;

@Component("editorCommitDao")
public class EditorCommitDaoImpl extends DaoSupport<EditorCommit> implements
		EditorCommitDao {

	public void add(String sql, Object... objs) {
		super.add(sql, objs);
	}

	public void update(String sql, Object... objs) {
		super.update(sql, objs);
	}

	public EditorCommit findById(int userId) {
		String sql = "select * from  t_editor_commit where userId=?";
		EditorCommit editorCommit = getJdbcTemplate().queryForObject(sql,
				new Object[] { userId }, new EditorCommitMapper());

		return editorCommit;
	}

	public List<EditorCommit> findByPeriodicalId(int periodicalId) {
		String sql = "select * from  t_editor_commit where periodicalId=?";
		List<EditorCommit> editorCommitList = super.getJdbcTemplate().query(
				sql, new Object[] { periodicalId }, new EditorCommitMapper());

		return editorCommitList;
	}
	
	@Override
	public boolean isAppliedCommit(int userId) {
		String sql = "select count(*) from t_editor_commit where userId=?";
		@SuppressWarnings("deprecation")
		int result = super.getJdbcTemplate().queryForInt(sql, new Object[]{ userId });
		if(result > 0) {
			return true;
		} else {
			return false;
		}
	}

	public Page<EditorCommit> findByProperty(String prop, Object value) {

		return null;
	}


	@Override
	public List<EditorCommit> getEditorCommits(String sql,int articalId) {
		// TODO Auto-generated method stub
		return getJdbcTemplate().query(sql, new Object[]{articalId}, new EditorCommitMapper());
	}


	@Override
	public List<EditorCommit> findAllByEditorChiefId(int userId) {
		/*String sql = "SELECT * from ECUserPeriodical";
		
		List<EditorCommit> list = super.getJdbcTemplate().query(sql, new Object[]{userId}, new EditorCommitMapper());*/
		return null;
	}


}

final class EditorCommitMapper implements RowMapper<EditorCommit> {

	public EditorCommit mapRow(ResultSet rs, int rowNum) throws SQLException {
		EditorCommit editorCommit = new EditorCommit();
		editorCommit.setApplyDate(rs.getDate("applyDate"));
		editorCommit.setCheckedData(rs.getDate("checkedDate"));
		editorCommit.setCheckedReason(rs.getString("checkedReason"));
		editorCommit.setEditorCommitId(rs.getInt("editorCommitId"));
		editorCommit.setIntroduce(rs.getString("introduce"));
		editorCommit.setPeriodicalId(rs.getInt("periodicalId"));
		editorCommit.setReason(rs.getString("reason"));
		editorCommit.setResult(rs.getString("result"));
		editorCommit.setUserId(rs.getInt("userId"));

		return editorCommit;
	}
}